home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.4 Applications 1997 August / SGI IRIX 6.4 Applications 1997 August.iso / dist / mmailp.idb / usr / lib / Zmail / bin / metasend.z / metasend
Encoding:
Text File  |  1997-01-22  |  14.4 KB  |  500 lines

  1. #!/bin/csh -fb
  2. # (The "-fb" might need to be changed to "-f" on some systems)
  3. #
  4. # Copyright (c) 1991 Bell Communications Research, Inc. (Bellcore)
  5. # Permission to use, copy, modify, and distribute this material 
  6. # for any purpose and without fee is hereby granted, provided 
  7. # that the above copyright notice and this permission notice 
  8. # appear in all copies, and that the name of Bellcore not be 
  9. # used in advertising or publicity pertaining to this 
  10. # material without the specific, prior written permission 
  11. # of an authorized representative of Bellcore.  BELLCORE 
  12. # MAKES NO REPRESENTATIONS ABOUT THE ACCURACY OR SUITABILITY 
  13. # OF THIS MATERIAL FOR ANY PURPOSE.  IT IS PROVIDED "AS IS", 
  14. # WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
  15. #
  16.  
  17. if (! $?METAMAIL_TMPDIR) then
  18.     set METAMAIL_TMPDIR=/tmp
  19. endif
  20.  
  21. set hname = `hostname`
  22. set idctr=0
  23. set MustDelete=0
  24. set batchmode=0
  25. set multipart = 1
  26. if ($?SPLITSIZE) then
  27.     set splitsize=$SPLITSIZE
  28. else
  29.     set splitsize = 100000
  30. endif
  31. set outputfile = ""
  32. set multipartsubtype = "mixed"
  33. set cdescrip = "An object packed by metasend"
  34. set isentity=0
  35. set newid=`date | tr " :" "__"`
  36. set cid="<${newid}_$idctr@${hname}>"
  37. @ idctr++
  38.  
  39. while ($#argv > 0)
  40.     switch ("$1")
  41.         case "-S":
  42.             shift
  43.             if ($#argv == 0) then
  44.                 echo "-S requires a following argument, the SPLIT threshhold"
  45.                 exit -1
  46.             endif
  47.             set splitsize=$1
  48.             shift
  49.             breaksw
  50.         case "-b":
  51.             # Batch mode -- ALL args must be on command line
  52.             set batchmode = 1
  53.             shift
  54.             breaksw
  55.         case "-c":
  56.             shift
  57.             if ($#argv == 0) then
  58.                 echo "-c requires a following argument, the CC address"
  59.                 exit -1
  60.             endif
  61.             set cc="$1"
  62.             shift
  63.             breaksw
  64.         case "-i":
  65.             shift
  66.             if ($#argv == 0) then
  67.                 echo "-i requires a following argument, the Content-ID value"
  68.                 exit -1
  69.             endif
  70.             set cid=$1
  71.             shift
  72.             breaksw
  73.         case "-I":
  74.             shift
  75.             if ($#argv == 0) then
  76.                 echo "-I requires a following argument, the multipart Content-ID value"
  77.                 exit -1
  78.             endif
  79.             set multicid=$1
  80.             shift
  81.             breaksw
  82.         case "-s":
  83.             shift
  84.             if ($#argv == 0) then
  85.                 echo "-s requires a following argument, the SUBJECT"
  86.                 exit -1
  87.             endif
  88.             set subject="$1"
  89.             shift
  90.             breaksw
  91.         case "-t":
  92.             shift
  93.             if ($#argv == 0) then
  94.                 echo "-t requires a following argument, the TO address"
  95.                 exit -1
  96.             endif
  97.             set to="$1"
  98.             shift
  99.             breaksw
  100.         case "-F":
  101.             shift
  102.             if ($#argv == 0) then
  103.                 echo "-F requires a following argument, the FROM address"
  104.                 exit -1
  105.             endif
  106.             set from="$1"
  107.             shift
  108.             breaksw
  109.         case "-P":
  110.             shift
  111.             if ($#argv == 0) then
  112.                 echo "-P requires a following argument, the preamble file name"
  113.                 exit -1
  114.             endif
  115.             set preamblefile="$1"
  116.             shift
  117.             breaksw
  118.         case "-e":
  119.             shift
  120.             if ($#argv == 0) then
  121.                 echo "-e requires a following argument, the ENCODING value"
  122.                 exit -1
  123.             endif
  124.             set encode=$1
  125.             if ($encode == "base64") then
  126.         set encodingprog = "mimencode -b"
  127.             else if ($encode == "x-uue") then
  128.             set encodingprog = "uuencode mail-body"
  129.             else if ($encode == "7bit") then
  130.             set encodingprog = cat
  131.            else
  132.             set encodingprog = "mimencode -q"
  133.             set encode = "quoted-printable"
  134.            endif 
  135.            shift
  136.             breaksw
  137.         case "-f":
  138.             shift
  139.             if ($#argv == 0) then
  140.                 echo "-f requires a following argument, the DATA FILE"
  141.                 exit -1
  142.             endif
  143.             set datafile=$1
  144.             if (! -e  $datafile) then
  145.                 echo The file $datafile does not exist.
  146.                 exit -1
  147.             endif
  148.             shift
  149.             breaksw
  150.         case "-m":
  151.             shift
  152.             if ($#argv == 0) then
  153.                 echo "-m requires a following argument, the MIME CONTENT-TYPE"
  154.                 exit -1
  155.             endif
  156. #            set ctyperaw = $1
  157. #            set  ctype=`echo $ctyperaw | tr \" \'`
  158.     set ctype = "$1"
  159.     if (`echo $ctype | grep /` == "") then
  160.         echo "Invalid content-type specified"
  161.         exit -1
  162.     endif
  163.             shift
  164.             breaksw
  165.         case "-o":
  166.             shift
  167.             if ($#argv == 0) then
  168.                 echo "-o requires a following argument, the output file name"
  169.                 exit -1
  170.             endif
  171.             set outputfile="$1"
  172.             shift
  173.             breaksw
  174.         case "-/":
  175.             shift
  176.             if ($#argv == 0) then
  177.                 echo "-/ requires a following argument, the multipart subtype"
  178.                 exit -1
  179.             endif
  180.             set multipartsubtype="$1"
  181.             shift
  182.             breaksw
  183.         case "-D":
  184.             shift
  185.             if ($#argv == 0) then
  186.                 echo "-D requires a following argument, the Content-Description value"
  187.                 exit -1
  188.             endif
  189.             set cdescrip="$1"
  190.             shift
  191.             breaksw
  192.         case "-E":
  193.             set isentity=1
  194.             shift
  195.             breaksw
  196.         case "-z":
  197.             set MustDelete=1
  198.             shift
  199.             breaksw
  200.         case "-n":
  201.             if ($isentity) then
  202.               if (! $?encode) then 
  203.                 set encode=7bit
  204.                 set encodingprog=cat
  205.               endif
  206.               if (! $?ctype) then
  207.                   set ctype=bogus
  208.                   # Never used
  209.                endif
  210.     if ( ! $?datafile) then
  211.         echo One part is incomplete -- each part needs at least -f and -E
  212.         exit -1
  213.     endif
  214.             else 
  215.     if ( ! $?ctype || ! $?datafile) then
  216.         echo One part is incomplete -- each part needs at least -f and -m
  217.         exit -1
  218.     endif
  219.             endif
  220.     if (! $?encode) then
  221.             if ("$ctype" =~ text*) then
  222.                   set encodingprog = "mimencode -q"
  223.         set encode = "quoted-printable"
  224.             else
  225.                 set encode = base64
  226.                 set encodingprog = "mimencode -b"
  227.             endif
  228.     endif
  229.     set ctype${multipart} = "$ctype"
  230.     set datafile${multipart} = $datafile
  231.     set encode${multipart} = $encode
  232.     set cdescrip${multipart} = "$cdescrip"
  233.     set cid${multipart} = "$cid"
  234.     set isentity${multipart} = $isentity
  235.     set encodingprog${multipart} = "$encodingprog"
  236.     unset ctype
  237.     unset datafile
  238.     unset encode
  239.     unset encodingprog
  240.     set newid=`date | tr " :" "__"`
  241.     set cid="<${newid}_$idctr@${hname}>"
  242.     @ idctr++
  243.     set cdescrip = "An object packed by metasend"
  244.     set isentity=0
  245.             @  multipart++
  246.     shift
  247.     breaksw
  248.         default:
  249.             echo UNRECOGNIZED METASEND OPTION: $1
  250.             exit -1
  251.     endsw
  252. end
  253. if ($batchmode == 0) then
  254.     if (! $?to) then
  255.         echo -n "To: "
  256.         set to = $<
  257.     endif
  258.     if (! $?subject) then
  259.         echo -n "Subject: "
  260.         set subject = $<
  261.     endif
  262.     if (! $?cc) then
  263.         echo -n "CC: "
  264.         set cc = $<
  265.     endif
  266. anotherpart:
  267.     if (! $?ctype) then
  268.         echo -n "Content-type: "
  269. #        set ctyperaw = $<
  270. #        set  ctype=`echo $ctyperaw | tr \" \'`
  271.         set ctype = "$<"
  272.     endif
  273.     if (`echo $ctype | grep /` == "") then
  274.         echo "Invalid content-type specified"
  275.         unset ctype
  276.         goto anotherpart
  277.     endif
  278. #    if ($ctype != $ctyperaw)  echo Double  quotes not allowed, using $ctype
  279.     if (! $?datafile) then
  280.         getfile:
  281.         echo -n "Name of file containing $ctype data: "
  282.         set datafile = $<
  283.         if (! -e  $datafile) then
  284.             echo The file $datafile does not exist.
  285.             goto getfile
  286.         endif
  287.     endif
  288.     if (! $?encode) then
  289.         retry:
  290.         echo "Do you want to encode this data for sending through the mail?"
  291.         echo "  1 -- No, it is already in 7 bit ASCII"
  292.         echo "  2 -- Yes, encode in base64 (most efficient)"
  293.         echo "  3 -- Yes, encode in quoted-printable (less efficient, more readable)"
  294.         echo "  4 -- Yes, encode it using uuencode (not standard, being phased out)"
  295.         set encode=$<
  296.         switch ("$encode")
  297.             case 1:
  298.                 set encodingprog = cat
  299.       set encode = 7bit
  300.                 breaksw
  301.             case 2:
  302.                 set encodingprog = "mimencode -b"
  303.       set encode = base64
  304.                 breaksw
  305.             case 3:
  306.                 set encodingprog = "mimencode -q"
  307.       set encode = quoted-printable
  308.                 breaksw
  309.             case 4:
  310.                 set encodingprog = "uuencode $datafile"
  311.       set encode = x-uue
  312.                 breaksw
  313.             default:
  314.                 echo Unrecognized answer, please try again.
  315.                 goto retry
  316.         endsw
  317.     endif
  318.     echo -n "Do you want to include another file too (y/n) [n] ? "
  319.     set ans=$<
  320.     if  ("$ans" =~ y*) then
  321.     set ctype${multipart} = "$ctype"
  322.     set datafile${multipart} = $datafile
  323.     set encode${multipart} = $encode
  324.     set encodingprog${multipart} = "$encodingprog"
  325.     set cdescrip${multipart} = "$cdescrip"
  326.     set cid${multipart} = "$cid"
  327.     set isentity${multipart} = $isentity
  328.     set cdescrip = "An object packed by metasend"
  329.     set isentity=0
  330.     unset ctype
  331.     unset datafile
  332.     unset encode
  333.     unset encodingprog
  334.     set newid=`date | tr " :" "__"`
  335.     set cid="<${newid}_$idctr@${hname}>"
  336.     @ idctr++
  337.             @  multipart++
  338.             goto anotherpart
  339.      endif
  340. else
  341.     if ($outputfile == "") then
  342.       if (! $?to || ! $?subject || ! $?ctype || ! $?datafile) then
  343.         echo metasend: in batch mode, if output is not directed to a file,-t, -s, -f, and -m are all required
  344.         exit -1
  345.       endif
  346.     else
  347.       if (! $?datafile) then
  348.         echo metasend:  in batch mode, -f is required.
  349.       endif
  350.       if ($isentity) then
  351.         if (! $?ctype) then
  352.           set ctype=bogus
  353.           # will not be used anyway really
  354.         endif
  355.       endif
  356.       if (! $?ctype  ) then
  357.         echo metasend: in batch mode, with output directed to a file, -E or -m is required
  358.         exit -1
  359.       endif
  360.     endif    
  361.     if (! -e  $datafile) then
  362.         echo metasend:  The file $datafile does not exist
  363.         exit -1
  364.     endif
  365. #    if (! $?cc) set cc=""
  366. #     if ($ctype != $ctyperaw)  echo Double  quotes not allowed, using $ctype
  367.     if (! $?encode) then
  368.         if ("$ctype" =~ text*) then
  369.     set encodingprog = "mimencode -q"
  370.     set encode = "quoted-printable"
  371.         else
  372.             set encodingprog = "mimencode -b"
  373.             set encode = base64
  374.         endif
  375.     else if ($encode == "base64") then
  376.         set encodingprog = "mimencode -b"
  377.     else if ($encode == "x-uue") then
  378.         set encodingprog = "uuencode mail-body"
  379.     else if ($encode == "7bit") then
  380.         set encodingprog = cat
  381.     else
  382.         set encodingprog = "mimencode -q"
  383.         set encode = "quoted-printable"
  384.     endif
  385. endif
  386.  
  387. if (! $?encode) then
  388.         if ("$ctype" =~ text*) then
  389.     set encode = "quoted-printable"
  390.     set encodingprog = "mimencode -q"
  391.         else
  392.             set encode = base64
  393.             set encodingprog = "mimencode -b"
  394.         endif
  395. endif
  396. set ctype${multipart} = "$ctype"
  397. set datafile${multipart} = $datafile
  398. set encode${multipart} = $encode
  399. set cdescrip${multipart} = "$cdescrip"
  400. set cid${multipart} = "$cid"
  401. set isentity${multipart} = $isentity
  402. set encodingprog${multipart} = "$encodingprog"
  403.  
  404. set fname = ${METAMAIL_TMPDIR}/msend.$$
  405. echo "MIME-Version: 1.0" > $fname
  406. if ($?to) then
  407.     echo "To: " "$to" >> $fname
  408. endif
  409. if ($?subject) then
  410.     echo "Subject: " "$subject" >> $fname
  411. endif
  412. if ($?cc) then
  413.     echo "CC: " "$cc" >> $fname
  414. endif
  415. if ($?from) then
  416.     echo "From: " "$from" >> $fname
  417. endif
  418. if ($multipart > 1) then
  419.     set  boundary = ${hname}.$$.`date | tr "  " .`
  420.     set newid=`date | tr " :" "__"`
  421.     if ($?multicid) then
  422.         echo "Content-ID: $multicid"   >> $fname
  423.     else
  424.         echo "Content-ID: <${newid}_$idctr@${hname}>"   >> $fname
  425.         @ idctr++
  426.     endif
  427.     echo "Content-type: multipart/$multipartsubtype;" >> $fname
  428.     echo "    boundary="\"$boundary\" >> $fname
  429.     echo "" >> $fname
  430.     if ($?preamblefile) then
  431.         cat $preamblefile >> $fname
  432.     else
  433.         echo "This is  a multimedia message in MIME  format.  If you are reading this" >> $fname
  434.         echo "prefix, your mail reader does  not understand MIME.  You may wish" >> $fname
  435.         echo "to look into upgrading to a newer version of  your mail reader." >> $fname
  436.     endif
  437.     set i=1
  438.     set junkfile=${METAMAIL_TMPDIR}/mmjunk.$$
  439.     while  ($multipart >= $i)
  440.     echo "" >> $fname
  441.     echo --$boundary >> $fname
  442.     echo set isentity=\$isentity$i > $junkfile
  443.     source $junkfile
  444.     if (! $isentity) then
  445.         echo -n "Content-ID: "   >> $fname
  446.         echo echo \$cid$i \>\>  $fname > $junkfile
  447.         source $junkfile 
  448.         echo -n "Content-type: ">>$fname
  449.         echo echo \$ctype$i \>\>  $fname > $junkfile
  450.         source $junkfile 
  451.         echo -n "Content-Description: ">>$fname
  452.         echo echo \"\$cdescrip$i \" \>\>  $fname > $junkfile
  453.         source $junkfile 
  454.         echo -n "Content-Transfer-Encoding: " >>$fname
  455.         echo echo \$encode$i \>\> $fname > $junkfile
  456.         source $junkfile 
  457.         echo ""  >> $fname
  458.     endif
  459.     echo \$encodingprog$i \< \$datafile$i \>\> $fname > $junkfile
  460.     source $junkfile 
  461.     @ i++
  462.     end
  463.     rm $junkfile
  464.     echo "" >> $fname
  465.     echo --${boundary}-- >> $fname
  466.     echo "" >> $fname
  467. else 
  468.     if (! $isentity) then
  469.         echo "Content-ID: $cid"   >> $fname
  470.         echo "Content-type: " $ctype >> $fname
  471.         echo "Content-Description: " $cdescrip >> $fname
  472.         echo "Content-Transfer-Encoding: " $encode >> $fname
  473.     endif
  474.     echo  "" >> $fname
  475.     $encodingprog < $datafile >> $fname
  476.     # Ensure last line has trailing carriage return
  477.     echo "" >> $fname
  478. endif
  479. if ($outputfile == "") then
  480.     if ($batchmode == 0) echo -n "Delivering mail, please wait...  "
  481.     splitmail -s $splitsize -d $fname
  482.     if (! $status) then
  483.         if ($batchmode == 0) echo "Mail delivery apparently succeeded."
  484.         rm $fname
  485.     else if ($MustDelete == 1) then
  486.         echo Mail delivery failed
  487.         rm $fname
  488.         exit -1
  489.     else
  490.         echo Mail delivery failed, draft mail is in $fname
  491.         exit -1
  492.     endif
  493. else
  494.     mv $fname $outputfile
  495.     if ($status) exit -1
  496. endif
  497. exit 0
  498.  
  499.